<?php

    include 'check.php';
	include 'db.php';

	function init(){
		$data 	= checkParams();
		$check 	= new check();
		$data['status'] = $check->checkurl($data);
		addFiles($data);
		echo 'success';
	}

	function checkParams(){
		$data = array(
			'url' 		=> $_GET['url'],
			'siteid' 	=> $_GET['siteid'],
			'fileid' 	=> $_GET['fileid']
		);
		if(empty($data['url']) || empty($data['siteid']) || empty($data['fileid'])){
			echo 'params_error';
			exit();
		}
		return $data;
	}

	function addFiles($data){
		$db 		= new db();
		$sql 		= 'select * from `git_files` where `files_id` = ' . $data['fileid'];
		$record 	= $db->getRecord($sql);
		if(!empty($record)){
			if($record['status'] == $data['status']){
				echo 'files exist';
				exit();
			}else{
				$sql = 'update `git_files` set `status` = ' . $data['status'] . ' where `id` = ' . $data['fileid'];
				$db->query($sql);
			}
			return;
		}
		$fileData 	= array(
			'files_id' 	=> $data['fileid'],
			'status' 	=> $data['status']
		);
		if(!$db->addRecord('git_files', $fileData)){
			echo 'add_error';
			exit();
		}
	}

	init();
?>